﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GrandExchangeTool.DataModel;
using System.Net;
using Newtonsoft.Json;

namespace GrandExchangeTool
{
    public partial class MainForm : Form
    {
        private const string BaseUrl = "http://services.runescape.com/m=itemdb_rs/api/catalogue/detail.json?item=";
        private IdProvider ids;
        private InfoLoader loader;
        private WebClient web = new WebClient();

        public MainForm()
        {
            InitializeComponent();
            InializeDatabase();
        }

        private void InializeDatabase()
        {
            ids = new IdProvider("ItemList.sqlite");
            loader = new InfoLoader(this);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            listBox1.Items.Clear();
            string[] list = ids.GetItems(textBox1.Text).ToArray();
            foreach (string s in list)
            {
                string _s = s.Replace("#$", "'");
                listBox1.Items.Add(_s);
            }
        }

        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar != (char)Keys.Enter)
            {
                return;
            }
            e.Handled = true;
            button1_Click(sender, null);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Not yet implemented");
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            loadItem(ids.GetId(listBox1.GetItemText(listBox1.SelectedItem)).ToString().Replace("'", "#$"));
        }

        //Get item async
        private void UpdateItem(ItemObject i, bool imageUpdate)
        {
            if (imageUpdate != false) pictureItem.LoadAsync(i.RootObject.Icon.Replace("sprite", "big"));
            this.label180Change.Text = "180 Day Change: " + i.RootObject.Day180.Change;
            this.label30Change.Text = "30 Day Change: " + i.RootObject.Day30.Change;
            this.label90Change.Text = "90 Day Change: " + i.RootObject.Day90.Change;
            this.labelItemDesc.Text = i.RootObject.Description;
            this.labelItemName.Text = i.RootObject.Name;
            this.labelPrice.Text = "Current Price: " + i.RootObject.Current.Price;
            this.labelTodayChange.Text = "Today's Change: " + i.RootObject.Today.Price;
        }

        public Item Deserialize(string JsonFile)
        {
            return JsonConvert.DeserializeObject<RootObject>(JsonFile).Item;
        }

        public void loadItem(string id)
        {
            if (web.IsBusy)
            {
                web.CancelAsync();
            }

            // Specify that the DownloadStringCallback2 method gets called
            // when the download completes.
            web.DownloadStringCompleted += new DownloadStringCompletedEventHandler(loadHTMLCallback);
            web.DownloadStringAsync(new Uri(BaseUrl + id));
        }

        public void loadHTMLCallback(Object sender, DownloadStringCompletedEventArgs e)
        {
            // If the request was not canceled and did not throw
            // an exception, display the resource.
            if (!e.Cancelled && e.Error == null)
            {
                UpdateItem(new ItemObject(Deserialize((string)e.Result)), true);
            }
        }
    }
}
